package com.approval.approvalapp.myself.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.Day;
import com.approval.approvalapp.bean.WdkqListBean;
import com.approval.approvalapp.util.DataUtilGetTime;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.core.CenterPopupView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import androidx.annotation.NonNull;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WdkqActivity extends BaseActivity {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.state)
    TextView state;
    @Bind(R.id.state_ll)
    LinearLayout stateLl;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.state_iv)
    ImageView stateIv;
    @Bind(R.id.jr_tv)
    TextView jrTv;
    @Bind(R.id.iv1)
    ImageView iv1;
    @Bind(R.id.jr_rl)
    RelativeLayout jrRl;
    @Bind(R.id.bz_tv)
    TextView bzTv;
    @Bind(R.id.iv2)
    ImageView iv2;
    @Bind(R.id.bz_rl)
    RelativeLayout bzRl;
    @Bind(R.id.by_tv)
    TextView byTv;
    @Bind(R.id.iv3)
    ImageView iv3;
    @Bind(R.id.by_rl)
    RelativeLayout byRl;
    @Bind(R.id.zdy_tv)
    TextView zdyTv;
    @Bind(R.id.kssj_tv)
    TextView kssjTv;
    @Bind(R.id.jssjtv)
    TextView jssjtv;
    @Bind(R.id.qd)
    TextView qd;
    @Bind(R.id.gray_view)
    View grayView;
    @Bind(R.id.menu_ll)
    LinearLayout menuLl;
    private List<WdkqListBean> wdkqListBeans = new ArrayList<>();
    @Bind(R.id.empty_ll)
    LinearLayout em;

    int statenum = 2;//0今日 1 本周 2本月 3自定义
    private long kstime;
    private String kstimestr;
    private long jstime;
    private String jstimestr;
    private RvAdapter rvAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_wdkq;
    }

    @Override
    public void initView() {
        refresh();
    }

    private void refresh() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("region_code", SpUtil.getString(this, SpUtil
                .REGION_CODE, ""));
        if (statenum == 0) {
            jsonObject.put("start", DataUtilGetTime.getTodayStart());
            jsonObject.put("end", DataUtilGetTime.getTodayEnd());
        }
        if (statenum == 1) {
            jsonObject.put("start", DataUtilGetTime.getWeekStart());
            jsonObject.put("end", DataUtilGetTime.getTodayEnd());
        }
        if (statenum == 3) {
            jsonObject.put("start", kstimestr);
            jsonObject.put("end", jstimestr);
        }

        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/attendance", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                wdkqListBeans = JSON.parseArray(str, WdkqListBean.class);
                rv.setLayoutManager(new LinearLayoutManager(WdkqActivity.this));
                rvAdapter = new RvAdapter();
                rvAdapter.setWdkqListBeans(wdkqListBeans);
                rv.setAdapter(rvAdapter);
                if (wdkqListBeans == null || wdkqListBeans.size() == 0){
                    em.setVisibility(View.VISIBLE);
                }else {
                    em.setVisibility(View.GONE);
                }
            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {

            }
        });
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.back_iv, R.id.state_ll, R.id.jr_rl, R.id.bz_rl, R.id.by_rl, R.id.kssj_tv, R.id.jssjtv, R.id.qd, R.id.gray_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.state_ll:
                if (menuLl.getVisibility() == menuLl.VISIBLE) {
                    menuLl.setVisibility(View.GONE);
                } else {
                    menuLl.setVisibility(View.VISIBLE);

                    if (statenum == 0) {
                        iv1.setVisibility(View.VISIBLE);
                        iv2.setVisibility(View.GONE);
                        iv3.setVisibility(View.GONE);
                        jrTv.setTextColor(Color.parseColor("#FF1465FD"));
                        bzTv.setTextColor(Color.parseColor("#333333"));
                        byTv.setTextColor(Color.parseColor("#333333"));

                        qd.setBackgroundResource(R.drawable.fafafashape4);
                        qd.setTextColor(Color.parseColor("#FF999999"));
                        zdyTv.setTextColor(Color.parseColor("#333333"));

                        kstimestr = "";
                        jstimestr = "";

                        kssjTv.setText("选择时间");
                        jssjtv.setText("选择时间");


                    } else if (statenum == 1) {
                        iv1.setVisibility(View.GONE);
                        iv2.setVisibility(View.VISIBLE);
                        iv3.setVisibility(View.GONE);
                        jrTv.setTextColor(Color.parseColor("#333333"));
                        bzTv.setTextColor(Color.parseColor("#FF1465FD"));
                        byTv.setTextColor(Color.parseColor("#333333"));

                        qd.setBackgroundResource(R.drawable.fafafashape4);
                        qd.setTextColor(Color.parseColor("#FF999999"));
                        zdyTv.setTextColor(Color.parseColor("#333333"));

                        kstimestr = "";
                        jstimestr = "";

                        kssjTv.setText("选择时间");
                        jssjtv.setText("选择时间");


                    } else if (statenum == 2) {
                        iv1.setVisibility(View.GONE);
                        iv2.setVisibility(View.GONE);
                        iv3.setVisibility(View.VISIBLE);
                        jrTv.setTextColor(Color.parseColor("#333333"));
                        bzTv.setTextColor(Color.parseColor("#333333"));
                        byTv.setTextColor(Color.parseColor("#FF1465FD"));

                        qd.setBackgroundResource(R.drawable.fafafashape4);
                        qd.setTextColor(Color.parseColor("#FF999999"));
                        zdyTv.setTextColor(Color.parseColor("#333333"));

                        kstimestr = "";
                        jstimestr = "";

                        kssjTv.setText("选择时间");
                        jssjtv.setText("选择时间");

                    } else {
                        iv1.setVisibility(View.GONE);
                        iv2.setVisibility(View.GONE);
                        iv3.setVisibility(View.GONE);
                        jrTv.setTextColor(Color.parseColor("#333333"));
                        bzTv.setTextColor(Color.parseColor("#333333"));
                        byTv.setTextColor(Color.parseColor("#333333"));

                        qd.setBackgroundResource(R.drawable.shapebule4);
                        qd.setTextColor(Color.parseColor("#ffffff"));
                        zdyTv.setTextColor(Color.parseColor("#FF1465FD"));
                    }
                }
                break;
            case R.id.jr_rl:
                statenum = 0;
                if (menuLl.getVisibility() == menuLl.VISIBLE) {
                    menuLl.setVisibility(View.GONE);
                }
                refresh();
                break;
            case R.id.bz_rl:
                statenum = 1;
                if (menuLl.getVisibility() == menuLl.VISIBLE) {
                    menuLl.setVisibility(View.GONE);
                }
                refresh();
                break;
            case R.id.by_rl:
                statenum = 2;
                if (menuLl.getVisibility() == menuLl.VISIBLE) {
                    menuLl.setVisibility(View.GONE);
                }
                refresh();
                break;
            case R.id.kssj_tv:
                TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {

                        kstime = date.getTime() / 1000;
                        kstimestr = kstime + "";

                        SimpleDateFormat format = new SimpleDateFormat("yyyy MM-dd");
                        kssjTv.setText(format.format(date));


                        if (!TextUtils.isEmpty(jstimestr)) {
                            //如果已经选择了结束时间 需要判断 开始时间是否小于结束时间
                            if (kstime > jstime) {
                                kstimestr = "";
                                Toast.makeText(WdkqActivity.this, "开始时间不能大于结束时间", Toast.LENGTH_SHORT).show();
                                kssjTv.setText("选择时间");
                                return;
                            }
                        }


                    }
                }).setType(new boolean[]{true, true, true, false, false, false})
                        .setTitleText("选择开始时间").setRangDate(null, Calendar.getInstance()).build();
                pvTime.show();
                break;
            case R.id.jssjtv:
                TimePickerView pvTime1 = new TimePickerBuilder(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {

                        jstime = date.getTime() / 1000;
                        jstimestr = jstime + "";


                        SimpleDateFormat format = new SimpleDateFormat("yyyy MM-dd");
                        jssjtv.setText(format.format(date));

                        if (!TextUtils.isEmpty(kstimestr)) {
                            //如果已经选择了开始时间 需要判断 结束时间是否小于开始时间
                            if (kstime > jstime) {
                                jstimestr = "";
                                jssjtv.setText("选择时间");
                                Toast.makeText(WdkqActivity.this, "结束时间不能小于结束时间", Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }

                    }
                }).setRangDate(null, Calendar.getInstance()).setType(new boolean[]{true, true, true, false, false, false})
                        .setTitleText("选择结束时间").build();
                pvTime1.show();
                break;
            case R.id.qd:
                statenum = 3;
                if (TextUtils.isEmpty(kstimestr) || TextUtils.isEmpty(jstimestr)) {
                    Toast.makeText(this, "请选择时间", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (menuLl.getVisibility() == menuLl.VISIBLE) {
                    menuLl.setVisibility(View.GONE);
                }

                refresh();

                break;
            case R.id.gray_view:
                if (menuLl.getVisibility() == menuLl.VISIBLE) {
                    menuLl.setVisibility(View.GONE);
                }
                break;
        }
    }

    class RvAdapter extends RecyclerView.Adapter<RvAdapter.Holder> {

        private List<WdkqListBean> wdkqListBeans;

        public void setWdkqListBeans(List<WdkqListBean> wdkqListBeans) {
            this.wdkqListBeans = wdkqListBeans;
            notifyDataSetChanged();
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(WdkqActivity.this).inflate(R.layout.item_wdkq, parent, false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.nameTv.setText(SpUtil.getString(WdkqActivity.this, SpUtil.USER_RUN_NICKNAME,
                    ""));
            holder.timeTv.setText(wdkqListBeans.get(position).getQDtime());
            if (TextUtils.isEmpty(wdkqListBeans.get(position).getZaoShangQD())) {
                holder.sbIv.setVisibility(View.VISIBLE);
                holder.sbTv.setVisibility(View.GONE);
            } else {
                holder.sbIv.setVisibility(View.GONE);
                holder.sbTv.setVisibility(View.VISIBLE);
                holder.sbTv.setText(wdkqListBeans.get(position).getZaoShangQD());
            }
            if (TextUtils.isEmpty(wdkqListBeans.get(position).getXiWuQD())) {
                holder.xbIv.setVisibility(View.VISIBLE);
                holder.xbTv.setVisibility(View.GONE);
            } else {
                holder.xbIv.setVisibility(View.GONE);
                holder.xbTv.setVisibility(View.VISIBLE);
                holder.xbTv.setText(wdkqListBeans.get(position).getXiWuQD());
            }

            if (wdkqListBeans.get(position).getStateT() == 0) {
                holder.statetv.setVisibility(View.GONE);
            } else {
                holder.statetv.setVisibility(View.VISIBLE);
            }

            if (wdkqListBeans.get(position).getStateT() == 1) {
                holder.statetv.setText("状态 : 正常");
                holder.statetv.setTextColor(Color.parseColor("#FF9CA0B5"));
                holder.ycqksm_ll.setVisibility(View.GONE);
            } else if (wdkqListBeans.get(position).getStateT() == 2) {
                holder.statetv.setText("状态 : 迟到");
                holder.statetv.setTextColor(Color.parseColor("#FFFB5A5A"));
                holder.ycqksm_ll.setVisibility(View.GONE);
            } else if (wdkqListBeans.get(position).getStateT() == 3) {
                holder.statetv.setText("状态 : 早退");
                holder.statetv.setTextColor(Color.parseColor("#FFFB5A5A"));
                holder.ycqksm_ll.setVisibility(View.GONE);
            } else if (wdkqListBeans.get(position).getStateT() == 4) {
                holder.statetv.setText("状态 : 旷工");
                holder.statetv.setTextColor(Color.parseColor("#FFFB5A5A"));
                holder.ycqksm_ll.setVisibility(View.VISIBLE);
            } else if (wdkqListBeans.get(position).getStateT() == 6) {
                holder.statetv.setText("状态 : 事假");
                holder.statetv.setTextColor(Color.parseColor("#FFFFA611"));
                holder.ycqksm_ll.setVisibility(View.GONE);
            } else if (wdkqListBeans.get(position).getStateT() == 7) {
                holder.statetv.setText("状态 : 病假");
                holder.statetv.setTextColor(Color.parseColor("#FFFFA611"));
                holder.ycqksm_ll.setVisibility(View.GONE);
            } else if (wdkqListBeans.get(position).getStateT() == 8) {
                holder.statetv.setText("状态 : 产假");
                holder.statetv.setTextColor(Color.parseColor("#FFFFA611"));
                holder.ycqksm_ll.setVisibility(View.GONE);
            } else if (wdkqListBeans.get(position).getStateT() == 9) {
                holder.statetv.setText("状态 : 公出");
                holder.statetv.setTextColor(Color.parseColor("#FFFFA611"));
                holder.ycqksm_ll.setVisibility(View.GONE);
            } else if (wdkqListBeans.get(position).getStateT() == 10) {
                holder.statetv.setText("状态 : 年休");
                holder.statetv.setTextColor(Color.parseColor("#FFFFA611"));
                holder.ycqksm_ll.setVisibility(View.GONE);
            } else if (wdkqListBeans.get(position).getStateT() == 15) {
                holder.statetv.setText("状态 : 异常情况已提交");
                holder.statetv.setTextColor(Color.parseColor("#FFFB5A5A"));
                holder.ycqksm_ll.setVisibility(View.GONE);
            }

            holder.ycqksm_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new XPopup.Builder(WdkqActivity.this)
                            .autoOpenSoftInput(true)
                            .asCustom(new CustomPopup(WdkqActivity.this, position, wdkqListBeans.get(position).getQDtime()))
                            .show();
                }
            });

        }

        @Override
        public int getItemCount() {
            if (wdkqListBeans == null) {
                return 0;
            } else {
                return wdkqListBeans.size();
            }
        }

        class Holder extends RecyclerView.ViewHolder {
            @Bind(R.id.name_tv)
            TextView nameTv;
            @Bind(R.id.time_tv)
            TextView timeTv;
            @Bind(R.id.sb_tv)
            TextView sbTv;
            @Bind(R.id.sb_iv)
            ImageView sbIv;
            @Bind(R.id.xb_tv)
            TextView xbTv;
            @Bind(R.id.xb_iv)
            ImageView xbIv;
            @Bind(R.id.statetv)
            TextView statetv;
            @Bind(R.id.ycqksm_ll)
            LinearLayout ycqksm_ll;

            public Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }


    private class CustomPopup extends CenterPopupView {

        String date;
        int index;

        public CustomPopup(@android.support.annotation.NonNull Context context, int index, String date) {
            super(context);
            this.date = date;
            this.index = index;
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_popup_new;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
            final EditText et = findViewById(R.id.et);
            ImageView close = findViewById(R.id.close);
            TextView tj = findViewById(R.id.tj);

            close.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

            tj.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(et.getText().toString())) {
                        Toast.makeText(WdkqActivity.this, "请填写内容", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("gh", SpUtil.getString(WdkqActivity.this, SpUtil.GH, ""));
                    long time = 0;
                    try {
                        Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(date);
                        time = parse.getTime();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    jsonObject.put("rq", time / 1000);
                    jsonObject.put("orgcode", SpUtil.getString(WdkqActivity.this, SpUtil.ORG_CODE, ""));
                    jsonObject.put("areacode", SpUtil.getString(WdkqActivity.this, SpUtil.REGION_CODE, ""));
                    jsonObject.put("content", et.getText().toString().trim());

                    Log.d("CustomPopupaaa", jsonObject.toString());

                    NetRequsetUtil.getInstance().netRequestPostJson("v1/center/exception_submit", jsonObject.toJSONString()
                            , new NetDownResponse() {
                                @Override
                                public void success(String str) {
                                    Toast.makeText(WdkqActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                    dismiss();
                                    refresState(index);
                                }

                                @Override
                                public void errowithresponse(String str) {
                                    ErrorBeanUtil.getErrorMessage(str);
                                    dismiss();
                                }

                                @Override
                                public void erro() {

                                }

                                @Override
                                public void finish() {

                                }
                            });
                }
            });
        }

    }

    public void refresState(int index) {
        wdkqListBeans.get(index).setStateT(15);
        rvAdapter.setWdkqListBeans(wdkqListBeans);
    }
}
